package com.dc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.ResponseResultVO;
import com.dc.entity.SparePart;
import com.dc.entity.ro.SparePartRO;
import com.dc.mapper.SparePartMapper;
import com.dc.service.SparePartService;
import com.dc.util.RedisUtil;
import com.dc.util.SnowflakeIdWorker;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.dc.common.ResponseResultVO.checkParameterIsNotNull;

@Service
public class SparePartServiceImpl extends ServiceImpl<SparePartMapper, SparePart> implements SparePartService {

    @Resource
    private SparePartMapper sparePartMapper;
    @Resource
    private RedisUtil redisUtil;


    @Override
    public ResponseResultVO insertSparePart(SparePartRO ro) {
        checkParameterIsNotNull(ro.getGoodsName(), "备件名称不能为空");
        checkParameterIsNotNull(ro.getSerialNumber(), "备件编号不能为空");
        checkParameterIsNotNull(ro.getSpecs(), "规格不能为空");
        checkParameterIsNotNull(ro.getUnitMeasure(), "计量单位不能为空");
        SparePart sparePart = new SparePart();
        BeanUtils.copyProperties(ro, sparePart);
        sparePart.setId(SnowflakeIdWorker.generateId());
        sparePart.setNumber(redisUtil.serialNumber("BJ"));
        return ResponseResultVO.insertResult(sparePartMapper.insert(sparePart));
    }

    @Override
    public ResponseResultVO updateSparePart(SparePartRO ro) {
        checkParameterIsNotNull(String.valueOf(ro.getId()), "id不能为空");
        SparePart sparePart = new SparePart();
        BeanUtils.copyProperties(ro, sparePart);
        return ResponseResultVO.updateResult(sparePartMapper.updateById(sparePart));
    }

    @Override
    public ResponseResultVO deleteSparePart(String ids) {
        checkParameterIsNotNull(ids, "id不能为空");
        List<String> idList = Arrays.asList(ids.split(","));
        List<SparePart> spareParts = sparePartMapper.selectList(new QueryWrapper<SparePart>().lambda().in(SparePart::getId, idList).gt(SparePart::getStockNum, 0));
        if (CollectionUtils.isNotEmpty(spareParts)) {
            ResponseResultVO.failResult("存在库存大于0的备件,不允许删除！");
        }
        return ResponseResultVO.deleteResult(sparePartMapper.deleteBatchIds(idList));
    }

    @Override
    public ResponseResultVO selectSparePartList(String name) {
        LambdaQueryWrapper<SparePart> queryWrapper = new QueryWrapper<SparePart>().lambda();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.eq(SparePart::getGoodsName, name);
        }
        return ResponseResultVO.successResult(sparePartMapper.selectList(queryWrapper));
    }

    @Override
    public ResponseResultVO selectSparePartListByPage(SparePartRO ro) {
        Page page = new Page<Map<String, Object>>(ro.getCurrentPage(), ro.getPageSize());
        LambdaQueryWrapper<SparePart> queryWrapper = new QueryWrapper<SparePart>().lambda();
        if (StringUtils.isNotBlank(ro.getGoodsName())) {
            queryWrapper.like(SparePart::getGoodsName, ro.getGoodsName());
        }
        if (StringUtils.isNotBlank(ro.getSerialNumber())) {
            queryWrapper.like(SparePart::getSerialNumber, ro.getSerialNumber());
        }
        queryWrapper.orderByDesc(SparePart::getCreateTime);
        return ResponseResultVO.resultList(sparePartMapper.selectPage(page, queryWrapper), ro);

    }
}
